package edu.suse.service.impl;

import edu.suse.common.base.RespResult;
import edu.suse.entity.SysRole;
import edu.suse.mapper.SysRoleMapper;
import edu.suse.service.SysRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public RespResult findSysRoleAllStatus(Long status) {
        List<SysRole> sysRoleList = sysRoleMapper.findSysRoleAllStatus(status);
        if (sysRoleList == null){
            return RespResult.error("不存在该角色或已删除");
        }
        return RespResult.success(sysRoleList);
    }

    @Override
    public RespResult findSysRoleByName(String name) {
        SysRole sysRole = sysRoleMapper.findSysRoleByName(name);
        return RespResult.success(sysRole);
    }

    @Override
    public RespResult findSysRoleByTitle(String title) {
        SysRole sysRole = sysRoleMapper.findSysRoleByTitle(title);
        if (sysRole == null){
            return RespResult.error("不存在该角色或已删除");
        }
        return RespResult.success(sysRole);
    }

    @Override
    public RespResult addSysRole(String title, String name, String remark) {
        SysRole sysRole = sysRoleMapper.findSysRoleByName(name);
        if (sysRole != null){
            return RespResult.error("已有该角色");
        } else {
            Timestamp createDate = new Timestamp(System.currentTimeMillis());
            Timestamp updateDate=createDate;

            Boolean success = sysRoleMapper.addSysRole(title, name, remark, createDate, updateDate);
            if (success){
                sysRole = sysRoleMapper.findSysRoleByName(name);
                return RespResult.success(sysRole);
            } else {
                return RespResult.error("增加失败, 估计是与数据库/sql相关的问题");
            }
        }
    }

    @Override
    public RespResult deleteSysRole(String name) {
        SysRole sysRole = sysRoleMapper.findSysRoleByName(name);
        if (sysRole != null){
            Long id = sysRole.getId();
            Boolean success = sysRoleMapper.deleteSysRole(id);
            if (success){
                return RespResult.success(sysRole);
            } else {
                return RespResult.error("删除失败, 估计是与数据库/sql相关的问题");
            }
        } else {
            return RespResult.error("不存在该角色或已删除");
        }
    }

    @Override
    public RespResult updateSysRole(Long roleId, String title, String name, String remark) {
        Timestamp updateDate = new Timestamp(System.currentTimeMillis());
        Boolean success = sysRoleMapper.updateSysRole(roleId, title, name, remark, updateDate);
        if (success){
            return RespResult.success();
        } else {
            return RespResult.error("更改角色信息失败, 估计是与数据库/sql相关的问题");
        }
    }

    @Override
    public RespResult updateSysRoleStatus(ArrayList<Long> roleIds, Long status) {
        Boolean success;
        for (Long id : roleIds) {
            success = sysRoleMapper.updateSysRoleStatus(id, status);
            if (!success){
                return RespResult.error("修改状态失败, 数据库或其他问题");
            }
        }
        return RespResult.success();
    }

}
